package main

import (
	"fmt"
	"html/template"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func formatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d/%02d/%02d", year, month, day)
}

func main() {
	// 启动一个 wsgi 应用
	router := gin.Default()
	// 自定义分隔符
	router.Delims("{[{", "}]}")
	// 模板函数
	router.SetFuncMap(template.FuncMap{
		"formatAsDate": formatAsDate,
	})
	// LoadHTMLFiles加载HTML文件的一部分，并将结果与HTML呈现器相关联。
	router.LoadHTMLFiles("day03/04gin_example/html_rendering/advanced/testdata/template/raw.tmpl")

	router.GET("/raw", func(c *gin.Context) {
		c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
			"now": time.Date(2018, 07, 01, 10, 20, 30, 0, time.UTC),
		})
	})

	router.Run(":8080")
}
